﻿using System.IO;
using System.Windows.Controls;
using GameEditor;
using GameEditor.View;
using GameEditor.View.Components;
using GameEngine.Model;
using GameEngine.Model.Data;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.Model.Data.implementation;
using GameEngine.View.Components;
using GameEngine;
using System;

namespace GameEditor.Commands
{
    public class GamePropertiesEditor_MenuAction_Command<MyIdType> : SimpleCommand
        where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            bool exit = false;
            var gameDataProxy = ModelFacade<MyIdType>.Instance.Game;
            var gameMediator = Facade.RetrieveMediator(GameEditorFacade.GameEditorMenuMediatorName) as GamePropertiesEditor_MenuMediator<MyIdType>;
            switch ((IGameEditorMenuViewConstants.ButtonTags)notification.Body)
            {
                case IGameEditorMenuViewConstants.ButtonTags.SET:
                    gameDataProxy.Name = gameMediator.Name;
                    gameDataProxy.TileSize = gameMediator.TileSize;
                    gameDataProxy.TileHeightUnit = gameMediator.TileHeightUnit;
                    gameDataProxy.MaxHeight = gameMediator.MaxMapHeight;
                    gameDataProxy.MinHeight = gameMediator.MinMapHeight;
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.CANCEL:
                    gameMediator.Name = gameDataProxy.Name;
                    gameMediator.TileSize = gameDataProxy.TileSize;
                    gameMediator.TileHeightUnit = gameDataProxy.TileHeightUnit;
                    gameMediator.MaxMapHeight = gameDataProxy.MaxHeight;
                    gameMediator.MinMapHeight = gameDataProxy.MinHeight;
                    break;
                #region Level
                case IGameEditorMenuViewConstants.ButtonTags.NEWLEVEL:
                    {
                        //ChildWindow wnd = new ChildWindow();
                        //var vo = SilverlightModelFacade.Instance.CreateLevelVO(new LevelVOId<int>(SilverlightModelFacade.Instance.CreateNewId(), "level"));
                        //var dataProxy = SilverlightModelFacade.Instance.CreateLevelDataProxy(vo);
                        //var med = SilverlightEditorViewFacade.Helper.CreateLevelPropertiesEditor(dataProxy);
                        //wnd.Content = med.ViewComponent;
                        //med.ViewComponent.OnExternalActionButtonClick += new OnButtonClickHandler<ILevelEditorMenuViewConstants.ButtonTags>(delegate(ILevelEditorMenuViewConstants.ButtonTags tag)
                        //{
                        //    switch ((ILevelEditorMenuViewConstants.ButtonTags)notification.Body)
                        //    {
                        //        case ILevelEditorMenuViewConstants.ButtonTags.OK:
                        //        case ILevelEditorMenuViewConstants.ButtonTags.CANCEL:
                        //            wnd.Close();
                        //            ApplicationFacade.Instance.RemoveMediator(med.MediatorName);
                        //            break;
                        //    }
                        //});
                        //wnd.Show();
                        //gameDataProxy.AddLevel(dataProxy);
                    }
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.EDITLEVEL:
                    {
                        //var dataProxy = ModelFacade<MyIdType>.Helper.GetLevelDataProxy(gameMediator.SelectedLevel);
                        //ChildWindow wnd = new ChildWindow();
                        //var med = SilverlightEditorViewFacade.Helper.CreateLevelPropertiesEditor(dataProxy);
                        //wnd.Content = med.ViewComponent;
                        //med.ViewComponent.OnExternalActionButtonClick += new OnButtonClickHandler<ILevelEditorMenuViewConstants.ButtonTags>(delegate(ILevelEditorMenuViewConstants.ButtonTags tag)
                        //{
                        //    switch ((ILevelEditorMenuViewConstants.ButtonTags)notification.Body)
                        //    {
                        //        case ILevelEditorMenuViewConstants.ButtonTags.OK:
                        //        case ILevelEditorMenuViewConstants.ButtonTags.CANCEL:
                        //            wnd.Close();
                        //            ApplicationFacade.Instance.RemoveMediator(med.MediatorName);
                        //            break;
                        //    }
                        //});
                        //wnd.Show();
                    }
                    exit = true;
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.REMOVELEVEL:
                    {
                        //var id = gameMediator.SelectedLevel;
                        //var dataProxy = SilverlightModelFacade.Helper.GetLevelDataProxy(id);
                        //gameDataProxy.RemoveLevel(id);
                        //SilverlightApplicationFacadeHelper.DestroyLevel(dataProxy.VO);
                    }
                    break;
                #endregion
                #region ThingType
                case IGameEditorMenuViewConstants.ButtonTags.NEWTHINGTYPE:
                    {
                        //ChildWindow wnd = new ChildWindow();
                        //var med = SilverlightEditorViewFacade.Helper.CreateThingTypePropertiesEditor();
                        //wnd.Content = med.ViewComponent;
                        //med.ViewComponent.OnExternalActionButtonClick += new OnButtonClickHandler<IThingTypeEditorMenuViewConstants.ButtonTags>(delegate(IThingTypeEditorMenuViewConstants.ButtonTags tag)
                        //{
                        //    switch ((IThingTypeEditorMenuViewConstants.ButtonTags)notification.Body)
                        //    {
                        //        case IThingTypeEditorMenuViewConstants.ButtonTags.OK:
                        //        case IThingTypeEditorMenuViewConstants.ButtonTags.CANCEL:
                        //            wnd.Close();
                        //            ApplicationFacade.Instance.RemoveMediator(med.MediatorName);
                        //            break;
                        //    }
                        //});
                        //wnd.Show();
                    }
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.EDITTHINGTYPE:
                    {
                        //var dataProxy = SilverlightModelFacade.Helper.GetThingTypeDataProxy(gameMediator.SelectedThingType);
                        //ChildWindow wnd = new ChildWindow();
                        ////TODOsenddataproxy
                        //var med = SilverlightEditorViewFacade.Helper.CreateThingTypePropertiesEditor();
                        //wnd.Content = med.ViewComponent;
                        //med.ViewComponent.OnExternalActionButtonClick += new OnButtonClickHandler<IThingTypeEditorMenuViewConstants.ButtonTags>(delegate(IThingTypeEditorMenuViewConstants.ButtonTags tag)
                        //{
                        //    switch ((ILevelEditorMenuViewConstants.ButtonTags)notification.Body)
                        //    {
                        //        case ILevelEditorMenuViewConstants.ButtonTags.OK:
                        //        case ILevelEditorMenuViewConstants.ButtonTags.CANCEL:
                        //            wnd.Close();
                        //            ApplicationFacade.Instance.RemoveMediator(med.MediatorName);
                        //            break;
                        //    }
                        //});
                        //wnd.Show();
                    }
                    exit = true;
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.REMOVETHINGTYPE:
                    {
                        var id = gameMediator.SelectedThingType;
                        var dataProxy = ModelFacade<MyIdType>.Helper.GetThingTypeDataProxy(id);
                        gameDataProxy.RemoveThingType(id);
                        ApplicationFacadeHelper<MyIdType>.DestroyThingType(dataProxy.VO);
                    }
                    break;
                #endregion
                #region View
                case IGameEditorMenuViewConstants.ButtonTags.NEWVIEW:
                    {
                        //ChildWindow wnd = new ChildWindow();
                        //var vo = SilverlightModelFacade.Instance.CreateViewVO(new ViewVOId<int>(SilverlightModelFacade.Instance.CreateNewId(), "view"));
                        //var dataProxy = SilverlightModelFacade.Instance.CreateViewDataProxy(vo);
                        //var med = SilverlightEditorViewFacade.Helper.CreateViewPropertiesEditor(dataProxy);
                        //wnd.Content = med.ViewComponent;
                        //med.ViewComponent.OnExternalActionButtonClick += new OnButtonClickHandler<IViewEditorMenuViewConstants.ButtonTags>(delegate(IViewEditorMenuViewConstants.ButtonTags tag)
                        //{
                        //    switch ((IViewEditorMenuViewConstants.ButtonTags)notification.Body)
                        //    {
                        //        case IViewEditorMenuViewConstants.ButtonTags.OK:
                        //        case IViewEditorMenuViewConstants.ButtonTags.CANCEL:
                        //            wnd.Close();
                        //            ApplicationFacade.Instance.RemoveMediator(med.MediatorName);
                        //            break;
                        //    }
                        //});
                        //wnd.Show();
                        //gameDataProxy.AddView(dataProxy);
                    }
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.EDITVIEW:
                    {
                        //var dataProxy = SilverlightModelFacade.Helper.GetViewDataProxy(gameMediator.SelectedView);
                        //ChildWindow wnd = new ChildWindow();
                        //var med = SilverlightEditorViewFacade.Helper.CreateViewPropertiesEditor(dataProxy);
                        //wnd.Content = med.ViewComponent;
                        //med.ViewComponent.OnExternalActionButtonClick += new OnButtonClickHandler<IViewEditorMenuViewConstants.ButtonTags>(delegate(IViewEditorMenuViewConstants.ButtonTags tag)
                        //{
                        //    switch ((IViewEditorMenuViewConstants.ButtonTags)notification.Body)
                        //    {
                        //        case IViewEditorMenuViewConstants.ButtonTags.OK:
                        //        case IViewEditorMenuViewConstants.ButtonTags.CANCEL:
                        //            wnd.Close();
                        //            ApplicationFacade.Instance.RemoveMediator(med.MediatorName);
                        //            break;
                        //    }
                        //});
                        //wnd.Show();
                    }
                    exit = true;
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.REMOVEVIEW:
                    {
                        var id = gameMediator.SelectedView;
                        var dataProxy = ModelFacade<MyIdType>.Helper.GetViewDataProxy(id);
                        gameDataProxy.RemoveView(id);
                        ApplicationFacadeHelper<MyIdType>.DestroyView(dataProxy.VO);
                    }
                    break;
                #endregion
                #region ViewState
                case IGameEditorMenuViewConstants.ButtonTags.NEWVIEWSTATE:
                    {
                        var id = gameMediator.SelectedViewState;
                        //mediator.AddViewState(id);
                    }
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.EDITVIEWSTATE:
                    {
                        //var dataProxy = SilverlightModelFacade.Helper.GetViewStateDataProxy(gameMediator.SelectedViewState);
                        //SendNotification(SilverlightEditorFacade<GameUC, LevelUC>.MY_PAGE_VIEWEDITOR_IN_COMMAND, dataProxy);
                    }
                    exit = true;
                    break;
                case IGameEditorMenuViewConstants.ButtonTags.REMOVEVIEWSTATE:
                    {
                        //var id = mediator.SelectedView;
                        //var dataProxy = SilverlightModelFacade.Helper.GetViewDataProxy(id);
                        //SilverlightApplicationFacadeHelper.DestroyView(dataProxy.VO);
                        //mediator.RemoveViewState(id);
                    }
                    break;
                #endregion
                //case IGameEditorMenuViewConstants.ButtonTags.NEWRESOURCEBUTTON:
                //    {
                //        var vo = new ResourceVO(new ResourceVOId("New Resource " + MyModelFacade.Instance.CreateNewId()));
                //        MyFacadeHelper.CreateResource(vo);
                //        var dataProxy = MyModelFacade.Helper.GetLevelDataProxy(vo);
                //        SendNotification(MyFacade.MY_PAGE_LEVELEDITOR_IN_COMMAND, dataProxy);
                //    }
                //    break;
                //case IGameEditorMenuViewConstants.ButtonTags.EDITRESOURCEBUTTON:
                //    {
                //        var dataProxy = MyModelFacade.Helper.GetResourceDataProxy(mediator.SelectedResource);
                //        SendNotification(MyFacade.MY_PAGE_RESOURCEEDITOR_IN_COMMAND, dataProxy);
                //    }
                //    break;
            }
            if (exit)
                Facade.RemoveMediator(GameEditorFacade.GameEditorMenuMediatorName);
        }
    }
}

